import time

from selenium.webdriver.common.by import By

from Pages import BasePage

class OBTHotelDetailPage(BasePage.Action):

    def __init__(self):
        #入住人勾选框--list，第0个是全部
        self.passengerCheckbox_xpath = (By.XPATH, "//div[@class='block checkin']//span[@class='el-checkbox__inner']")
        #改期按钮
        self.applyChange_xpath = (By.XPATH, "//button[@class='btn change']")
        #申请取消按钮
        self.applyRefund_xpath = (By.XPATH, "//button[@class='btn cancel']")
        #入住日期选择框
        self.datePicker_xpath = (By.XPATH, "//div[@class = 'datepicker']//input")
        #当前日期 高亮
        self.originDate_xpath = (By.XPATH, "//span[contains(@class, 'datepicker-dateRange-item-active')]")
        #改期原因
        self.changeReason_xpath = (By.XPATH, "//input[@placeholder='请选择改期原因']")
        self.reasonOne_xpath = (By.XPATH, "//li/span[text()='我是自愿改签的']")
        #
        self.confirmChange_xpath = (By.XPATH,"//button[text()='确定改期']")
        self.confirmRefund_xpath = (By.XPATH,"//button[text()='确定取消']")
        #
        self.submitButton_xpath = (By.XPATH, "//span[text()='确认提交']")
        #
        self.sureButton_xpath = (By.XPATH, "/html/body/div[3]/div/div[3]/button/span")
        #
        self.copyOrderNo_xpath = (By.XPATH, "//span[text()='复制']")
        #订单状态
        self.orderStatus_xpath = (By.XPATH, "//span[@class='order-status']")

    def choosePassenger(self):
        self.find_elements(*self.passengerCheckbox_xpath)[0].click()
        time.sleep(1)

    def chooseDate(self):
        self.smart_click(*self.datePicker_xpath)
        time.sleep(1)
        originDate = self.find_elements(*self.originDate_xpath)[0].get_attribute('data-date')
        #入住日期+1天，86400秒
        newCheckinDate = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(originDate, '%Y-%m-%d'))+86400))
        newCheckinDay_xpath = (By.XPATH, "//span[@data-date='%s' and (contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')))]" % newCheckinDate)
        self.find_elements(*newCheckinDay_xpath)[0].click()
        time.sleep(2)
        originDate = self.find_elements(*self.originDate_xpath)[1].get_attribute('data-date')
        print(originDate)
        #离店日期-1天，86400秒
        newCheckoutDate = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(originDate, '%Y-%m-%d'))-86400))
        print(newCheckoutDate)
        newCheckoutDay_xpath = (By.XPATH, "//span[@data-date='%s' and (contains(@class,'day-cell') and not(contains(@class,'datepicker-item-disabled')))]" % newCheckoutDate)
        print(newCheckoutDay_xpath)
        self.find_elements(*newCheckoutDay_xpath)[1].click()
        time.sleep(1)

    def getOrderNo(self):
        temp = self.find_element(*self.copyOrderNo_xpath).get_attribute("data-clipboard-text")
        return temp

    def applyChange(self):
        self.choosePassenger()
        self.smart_click(*self.applyChange_xpath)
        time.sleep(2)
        self.chooseDate()
        time.sleep(1)
        if (self.find_element(*self.changeReason_xpath)):
            self.find_element(*self.changeReason_xpath).click()
            self.find_element(*self.reasonOne_xpath).click()
            time.sleep(1)
        self.smart_click(*self.confirmChange_xpath)
        time.sleep(3)
        self.smart_click(*self.submitButton_xpath)
        time.sleep(2)
        self.smart_click(*self.sureButton_xpath)
        time.sleep(2)


    def getOrderStatus(self):
        self.refresh_window()
        time.sleep(2)
        orderStatus = self.find_element(*self.orderStatus_xpath).text
        return orderStatus

    def applyRefund(self):
        if self.getOrderStatus() in ("待提交", "退房中", "已取消", "已离店", "已退款"):
            #确认中状态分两种，预订确认中有可能可取消；改期确认中不可申请取消
            print("当前状态："+self.getOrderStatus()+"，不可申请取消")
            return
        else:
            self.choosePassenger()
            self.smart_click(*self.applyRefund_xpath)
            time.sleep(2)
            self.smart_click(*self.confirmRefund_xpath)
            time.sleep(2)
            self.smart_click(*self.submitButton_xpath)
            time.sleep(2)
            self.smart_click(*self.sureButton_xpath)
            time.sleep(10)
            return
